<template>
<Modal :title="title" ref='modal'
  v-model="visible"
  :width='width'
  scrollable
  :draggable="draggable"
  :fullscreen="fullscreen"
    >
  <div slot="header" class="ivu-modal-header-inner">
    <span>{{title}}</span>
    <a class="ivu-modal-close">
    <i v-if="minMax" style="font-size: 28px;" @click="minMaxHandler" :class="fullscreen?'ivu-icon ivu-icon-ios-photos-outline':'ivu-icon ivu-icon-ios-square-outline'"></i>
    <i @click="close" class="ivu-icon ivu-icon-ios-close"></i>
    </a>
  </div>
  <MldContent ref='mldContent' :params="params"></MldContent>
  <div slot="footer">
    <Button v-if="showCancel" @click='cancelHandler' size='large'>取消</Button>
    <Button v-if="showOk" :loading='buttonLoading' type="primary" size='large' @click='okHandler'>保存</Button>
 </div>
</Modal>
</template>
<script>
export default {
  data () {
    return {
      title: '',
      draggable: false,
      minMax: true,
      visible: true,
      fullscreen: false,
      width: 800,
      height: 400,
      top: 0,
      buttonLoading: false,
      parent: null,
      params: {},
      showCancel: true,
      showOk: true
    }
  },
  created () {
    // if (this.parent) {
    //   if (typeof this.parent.getCurrentRow === 'function') {
    //     this.params = this.parent.getCurrentRow()
    //   }
    // }
  },
  methods: {
    minMaxHandler () {
      this.fullscreen = !this.fullscreen
    },
    close () {
      this.visible = false
      this.remove()
    },
    remove: function () {
      setTimeout(() => {
        this.destroy()
      }, 300)
    },
    destroy: function () {
      this.$destroy()
      document.body.removeChild(this.$el)
    },
    okHandler () {
      let $this = this
      if (typeof this.$refs['mldContent'].submit === 'function') {
        $this.buttonLoading = true
        this.$refs['mldContent'].submit().then(res => {
          if (res.code === 0) {
            this.$Message.success(
              {
                content: res.msg ? (res.msg === 'ok' ? '保存成功' : res.msg) : '保存成功',
                onClose () {
                  $this.buttonLoading = false
                  $this.close()
                  if ($this.parent) {
                    if (typeof $this.parent.handleRefresh === 'function') {
                      $this.parent.handleRefresh($this.parent)
                    }
                  }
                }
              })
          } else {
            // this.$Message.error(res.msg)
            $this.buttonLoading = false
          }
        }).catch(e => {
          this.$Message.error(e.message)
          $this.buttonLoading = false
        })
      } else if (typeof this.$refs['mldContent'].selectCallback === 'function') {
        this.$refs['mldContent'].selectCallback(this.parent).then(res => {
          this.close()
        }).catch(e => {})
      } else {
        this.okx(this)
        this.close()
      }
    },
    cancelHandler () {
      this.cancel(this)
      this.close()
    }
  }
}
</script>
